package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.OrderService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchOrderServlet extends HttpServlet {
    private static final String SEARCH_ORDER = "/WEB-INF/jsp/order/searchOrder.jsp";

    private Account account;
    private Order order;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        account = (Account) session.getAttribute("account");
        OrderService orderService = new OrderService();
        List<Order> orderList = new ArrayList<>();
        orderList = orderService.getOrdersByUsername(account.getUsername());
        session.setAttribute("orderList", orderList);
        req.getRequestDispatcher(SEARCH_ORDER).forward(req, resp);
        /*order = (Order) session.getAttribute("order");
        if (order != null){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            order.setOrderDate(dateFormat.format(date));
            req.getRequestDispatcher(SEARCH_ORDER).forward(req, resp);
        }*/

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
